package com.kjlink.internal.workreport.jira.controller;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import cn.xuqiudong.common.base.model.Remind;
import com.kjlink.internal.workreport.base.controller.BaseController;
import com.kjlink.internal.workreport.base.enums.JiraCheckResult;
import com.kjlink.internal.workreport.jira.model.Jira;
import com.kjlink.internal.workreport.jira.service.JiraService;
import com.kjlink.internal.workreport.jira.vo.JiraReportDetail;
import com.kjlink.internal.workreport.project.service.ProjectService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 功能: :jira信息 控制层
 * <p>
 * {@link com.kjlink.internal.report4vue.jira.controller.ApiJiraController}
 *
 * @author Vic.xu
 * @since 2024-04-30 09:37
 */
@RestController
@RequestMapping("/jira")
public class JiraController extends BaseController<JiraService, Jira> {

    @Resource
    private ProjectService projectService;

    /**
     * 绑定一些通用的下拉框参数
     *
     * @param model
     */
    @ModelAttribute
    private void bindSelect(Model model) {
        model.addAttribute("checkResults", JiraCheckResult.values());
    }


    /**
     * 列表页
     */
    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView index(Jira lookup) {
        ModelAndView modelAndView = new ModelAndView("jira/list");
        PageInfo<Jira> pager = service.page(lookup);
        pager.setLookup(lookup);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    /**
     * 前往新增页面
     *
     * @return
     */
    @GetMapping("/form")
    public ModelAndView insert() {
        ModelAndView mv = new ModelAndView("jira/form");
        return mv;
    }

    /**
     * 前往新增页面
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id, RedirectAttributes attrs) {

        Jira entity = service.findById(id);
        if (entity == null) {
            attrs.addAttribute(Remind.danger().setMessage("不存在的jira"));
            return new ModelAndView("redirect:/jira/");
        }
        ModelAndView mv = new ModelAndView("jira/form");
        mv.addObject("entity", entity);
        return mv;
    }

    /**
     * jira 号是否在本系统存在
     */
    @RequestMapping("/checkCode")
    public BaseResponse<Boolean> checkCode(Integer id, String code) {
        boolean success = service.checkNotRepeat(id, code, "code");
        return BaseResponse.success(success);
    }

    /**
     * 检测jira号是否存在，先在本系统查询，后去jira系统查询
     */
    @RequestMapping("/exist")
    public BaseResponse<Boolean> exist(String code) {
        boolean exist = service.checkJira(code);
        return BaseResponse.success(exist);
    }


    @PostMapping("/save")
    public ModelAndView save(Jira entity, RedirectAttributes attrs) {
        ModelAndView mv = new ModelAndView("redirect:/jira/");
        service.save(entity);
        attrs.addFlashAttribute(Remind.success().setMessage("保存成功"));
        return mv;
    }

    @GetMapping("/import")
    public ModelAndView importJira() {
        ModelAndView mv = new ModelAndView("jira/import-jira");
        mv.addObject("projects", projectService.mineProject(null));
        return mv;
    }

    /**
     * 根据jira号导入jira
     */
    @PostMapping("/import")
    public BaseResponse<Jira> importJira(Integer projectId, String code,
                                         @RequestParam(defaultValue = "false") boolean force) {
        return service.createByCode(code, projectId, force);
    }


    @PostMapping("/delete/{id}")
    public ModelAndView deleteProjectUser(@PathVariable Integer id, RedirectAttributes attrs) {
        service.delete(id);
        return redirectHome(attrs);
    }

    /**
     * 根据jira号统计工时信息
     *
     * @param jira jira号
     */
    @GetMapping("/summary/{jira}")
    public ModelAndView summary(@PathVariable String jira, RedirectAttributes attr) {
        service.summaryUsedTime(jira);
        return redirectHome(attr);
    }

    @Override
    protected String home() {
        return "/jira/";
    }

    /**
     * jira的工时详情 并进行统计一次工时
     */
    @GetMapping("/reportDetail/{code}")
    public ModelAndView reportDetail(@PathVariable String code, String back, RedirectAttributes attr) {
        ModelAndView mv = new ModelAndView("jira/reportDetai");
        JiraReportDetail detail = service.summaryAndGetReportDetail(code);
        if (detail == null) {
            return redirectHome("不存在的jira", attr);
        }
        mv.addObject("back", back);
        mv.addObject("detail", detail);
        return mv;
    }


}

